﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace AngryBullets.Input
{
    /// <summary>
    /// Classe d'entrée utilisateur minimaliste
    /// </summary>
    public class InputState : GameComponent
    {
        #region Fields

        private static KeyboardState _currentKeyboardState;
        private static KeyboardState _lastKeyboardState;
        private static MouseState _currentMouseState;
        private static MouseState _lastMouseState;

        #endregion

        #region Properties

        public static KeyboardState CurrentKeyboardState
        {
            get { return _currentKeyboardState; }
        }

        public static MouseState CurrentMouseState
        {
            get { return _currentMouseState; }
        }

        #endregion

        #region Initialization

        public InputState(Game game)
            : base(game)
        {
            _currentKeyboardState = new KeyboardState();
            _lastKeyboardState = new KeyboardState();
            _currentMouseState = new MouseState();
            _lastMouseState = new MouseState();
        }

        #endregion

        #region Public Methods

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            _lastKeyboardState = _currentKeyboardState;
            _currentKeyboardState = Keyboard.GetState();
            _lastMouseState = _currentMouseState;
            _currentMouseState = Mouse.GetState();
        }

        private static bool IsNewKeyPress(Keys key)
        {
            return (_currentKeyboardState.IsKeyDown(key) &&
                    _lastKeyboardState.IsKeyUp(key));
        }

        public static bool IsMenuSelect()
        {
            return IsNewKeyPress(Keys.Space) ||
                   IsNewKeyPress(Keys.Enter);
        }

        public static bool IsMenuCancel()
        {
            return IsNewKeyPress(Keys.Escape);
        }

        public static bool IsMenuUp()
        {
            return IsNewKeyPress(Keys.Up);
        }

        public static bool IsMenuDown()
        {
            return IsNewKeyPress(Keys.Down);
        }

        public static bool IsPauseGame()
        {
            return IsNewKeyPress(Keys.Escape);
        }

        #endregion
    }
}
